#include "asynframeimpl.h"

#include "filereadimpl.h"
#include "filewriteimpl.h"
#include "tcpsocketimpl.h"
#include "asynctcpsocketimpl.h"

#include <thread>

uvNameSpace

CAsynFrameImpl::CAsynFrameImpl(void)
{
	m_asynframe_sem;

}

CAsynFrameImpl::~CAsynFrameImpl(void)
{
}
//////////////////////////////////////////////////////////////////////////
std_method_impl CAsynFrameImpl::init_class(IBase* prot, IBase* punkOuter)
{
	HRESULT		hr = S_OK;
	if (prot != NULL)
	{
		m_pRot = prot;
	}
	return hr;
}
std_method_impl CAsynFrameImpl::Init(void*)
{
	os_sem_init(&this->m_asynframe_sem, 0);
	return S_OK;
}
std_method_impl CAsynFrameImpl::Uninit()
{
	os_sem_destroy(&this->m_asynframe_sem);
	return S_OK;
}
std_method_impl CAsynFrameImpl::Start()
{
#if (TARGET_OS == OS_WINDOWS)
	WORD verision = MAKEWORD(2, 2);
	WSADATA lpData;
	int intEr = WSAStartup(verision, &lpData);
	if (intEr != 0)
	{
		return E_FAIL;
	}
#endif

	m_pThreadPool	= new 	io_thread_pool;

	ithread::start_thread();
	os_sem_wait(&this->m_asynframe_sem);

	iosprintf("asynio frame start")
	return S_OK;
}
std_method_impl CAsynFrameImpl::Stop()
{

	threadstop();
	ithread::join();
	iosprintf("asynio frame stop")

#if (TARGET_OS == OS_WINDOWS)

	if (WSACleanup() != 0)
	{
		return E_FAIL;
	}
#endif 


	return S_OK;
}
std_method_impl CAsynFrameImpl::AddTimer(ITimerEvents *pEvents,
	ULONG lTimerId,
	ULONG lMilliSecond,
	BOOL bCycled)
{
	return S_OK;
}
std_method_impl CAsynFrameImpl::DelTimer(ITimerEvents *pEvents, ULONG lTimerId)
{
	return S_OK;
}
std_method_impl CAsynFrameImpl::ClearTimer(ITimerEvents *pEvents)
{
	return S_OK;
}
std_method_impl CAsynFrameImpl::CreateAsynIoOperation(	IIoOperation **ppOperation,
														IIoOperationEvents* pEvent)
{
	CIoOperationImpl* Oper = NULL;
	Oper = new CIoOperationImpl;
	Oper->BindEvent(pEvent);
	return Oper->QueryInterface(_uuidof(IIoOperation), (void**)ppOperation);
}
std_method_impl CAsynFrameImpl::PostAsynIoOperation(IIoOperation *pOperation)
{
	return E_FAIL;
}
std_method_impl CAsynFrameImpl::CreateReadFile(IBase** pReadFile)
{
	CFileReadImpl*	pObject = NULL;
	pObject = new CFileReadImpl;
	return pObject->QueryInterface(_uuidof(IReadFile), (void**)pReadFile);;
}
std_method_impl CAsynFrameImpl::CreateWriteFile(IBase** pWriteFile)
{
	CFileWriteImpl*	pObject = NULL;
	pObject = new CFileWriteImpl;
	return pObject->QueryInterface(_uuidof(IWriteFile), (void**)pWriteFile);
}
std_method_impl CAsynFrameImpl::CreateAsyncTcpSocket(IBase** pAsynTcpSocket)
{
	CAsyncTcpSocketImpl*	pObject = NULL;
	pObject = new CAsyncTcpSocketImpl;
	return pObject->QueryInterface(_uuidof(IAsyncTcpSocket), (void**)pAsynTcpSocket);
}
std_method_impl CAsynFrameImpl::CreateTcpSocket(IBase** pTcpSocket)
{
	CTcpSocketImpl*	pObject = NULL;
	pObject = new CTcpSocketImpl;
	return 	pObject->QueryInterface(_uuidof(ITcpSocket), (void**)pTcpSocket);
}
std_method_impl CAsynFrameImpl::CreateAsyncUdpSocket(IBase** pAsynUdpSocket)
{
	HRESULT		hr = S_OK;
	return hr;
}
std_method_impl CAsynFrameImpl::CreateUdpSocket(IBase** pUdpSocket)
{
	HRESULT		hr = S_OK;
	return hr;
}

std_method_impl CAsynFrameImpl::DeleteIo(IBase* pAsynIo)
{
	HRESULT		hr = S_OK;
	if (pAsynIo != NULL)
	{
		pAsynIo->Release();
	}
	return hr;
}
void CAsynFrameImpl::run()
{
#if (TARGET_OS == OS_WINDOWS)
	init_threadpool(m_pThreadPool, std::thread::hardware_concurrency(), this);
#elif (TARGET_OS == OS_POSIX)
	init_threadpool(m_pThreadPool, std::thread::hardware_concurrency(), this);
#elif (TARGET_OS == OS_MAC)
	init_threadpool(m_pThreadPool, std::thread::hardware_concurrency(), this);
#endif
	run_threadpool(m_pThreadPool,CAsynFrameImpl::StartCBFun, CAsynFrameImpl::StopCBFun);
}
void CAsynFrameImpl::threadstop()
{
	iosprintf("stoping event loop......start")
	stop_threadpool(m_pThreadPool);
	iosprintf("stoping event loop......end")
}
void CAsynFrameImpl::SetService(bool bstart)
{
	iosprintf("CAsynFrameImpl::SetService")
	os_sem_post(&this->m_asynframe_sem);

}
void CAsynFrameImpl::StartCBFun(void *pData)
{
	iosprintf("CAsynFrameImpl::StartCBFun")
	CAsynFrameImpl* pThis = static_cast<CAsynFrameImpl*>(pData);
	if(pThis != NULL)
		pThis->SetService(true);


}
void CAsynFrameImpl::StopCBFun(void *pData)
{
	
}

